#include "weldtoolview.h"
#include "ui_weldtoolview.h"
#include <QDebug>
#include <QMessageBox>

WeldToolView::WeldToolView(CommunicateNormalDelegate *communicateNormalDelegateIn, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WeldToolView)
{
    ui->setupUi(this);
    communicateNormalDelegate=communicateNormalDelegateIn;
    updateTimer = new QTimer;
    connect(updateTimer,SIGNAL(timeout()),this,SLOT(timeWork()));
    on_checkBox_isEnableOperation_clicked(false);
}

WeldToolView::~WeldToolView()
{
    delete ui;
}

void WeldToolView::showDialog(bool isShow)
{
    if(isShow)
    {
        updateTimer->start(50);
    }
    else
    {
        updateTimer->stop();
    }

}

void WeldToolView::timeWork()
{
//    qDebug()<<"WeldToolView::timeWork";
//    if(false==communicateNormalDelegate->isConnectedStatus())
//    {
//        return ;
//    }
    communicateNormalDelegate->getWeldDebugStatusInfo(infoOut);
    ui->checkBox_virtualWeldStatus->setChecked(infoOut.isVirtualControl);
    ui->checkBox_enableCollisionCheckStatus->setChecked(infoOut.enableCollisionCheckStatus);
    ui->checkBox_jogFeedStatus->setChecked(infoOut.jogFeedStatus);
    ui->checkBox_weldSucessStatus->setChecked(infoOut.weldSucess);
    ui->checkBox_collisionStatus->setChecked(infoOut.collisionStatus);
    ui->label_weldVoltage->setText(QString::number(infoOut.weldVoltage,'f',1));
    ui->label_weldCurrency->setText(QString::number(infoOut.weldCurrency,'f',1));
    ui->label_weldTime->setText(QString::number(infoOut.weldTime,'f',1));
    ui->label_weldPWM->setText(QString::number(infoOut.weldPwm,'f',1));
}



void WeldToolView::on_pushButton_feedWire_pressed()
{

    int returnFlag;
    communicateNormalDelegate->weldDebugOperation(E_WELD_TEST_FEEDWIRE,true,returnFlag);
}

void WeldToolView::on_pushButton_feedWire_released()
{
    int returnFlag;
    communicateNormalDelegate->weldDebugOperation(E_WELD_TEST_FEEDWIRE,false,returnFlag);
}

void WeldToolView::on_pushButton_withdrawWire_pressed()
{
    int returnFlag;
    communicateNormalDelegate->weldDebugOperation(E_WELD_TEST_WITHDRAWWIRE,true,returnFlag);
}

void WeldToolView::on_pushButton_withdrawWire_released()
{
    int returnFlag;
    communicateNormalDelegate->weldDebugOperation(E_WELD_TEST_WITHDRAWWIRE,false,returnFlag);
}

void WeldToolView::on_pushButton_gasOn_pressed()
{
    int returnFlag;
    communicateNormalDelegate->weldDebugOperation(E_WELD_TEST_GAS,true,returnFlag);
}

void WeldToolView::on_pushButton_gasOn_released()
{
    int returnFlag;
    communicateNormalDelegate->weldDebugOperation(E_WELD_TEST_GAS,false,returnFlag);
}


void WeldToolView::on_pushButton_changeVirtualWeld_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否切换焊接模拟状态！"
                        , QMessageBox::Yes|QMessageBox::No , this);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
//    communicateNormal->getWeldIsVirtualStatus(returnFlag);
    bool isVirtual;
    if(1==infoOut.isVirtualControl)
    {
        isVirtual=false;
    }
    else
    {
        isVirtual=true;
    }
    communicateNormalDelegate->setWeldVirtualMode(isVirtual,returnFlag);
}

void WeldToolView::on_pushButton_changeCollisionCheck_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否碰撞检查状态！"
                        , QMessageBox::Yes|QMessageBox::No , this);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int returnFlag;
//    communicateNormal->getWeldIsVirtualStatus(returnFlag);
    bool isVirtual;
    if(1==infoOut.enableCollisionCheckStatus)
    {
        isVirtual=false;
    }
    else
    {
        isVirtual=true;
    }
    communicateNormalDelegate->setWeldCollisionCheck(isVirtual,returnFlag);
}

void WeldToolView::on_checkBox_isEnableOperation_clicked(bool checked)
{

    ui->pushButton_feedWire->setEnabled(checked);
    ui->pushButton_withdrawWire->setEnabled(checked);
    ui->pushButton_gasOn->setEnabled(checked);
    ui->pushButton_spotWeld->setEnabled(checked);
    ui->pushButton_changeVirtualWeld->setEnabled(checked);
    ui->pushButton_changeCollisionCheck->setEnabled(checked);

}
